<template>
  <div>
    <div class="qiehuan">
      <div class="name" @click="showKong = true">
        <i class="mr5">{{ subject_name }}</i>
        <van-icon :name="require('@/assets/images/course-qiehuan.png')" />
      </div>
      <van-icon
        v-if="['/course-home', '/course-home/'].indexOf(this.$route.path) >= 0"
        name="search"
      />
    </div>
    <!--选择科目 -->
    <div class="subject_popup">
      <van-popup v-model="showKong" closeable position="bottom">
        <div v-for="(item, index) in subjectList" :key="index">
          <div class="headline">
            <img
              class="headline_ico"
              :src="require('@/assets/images/subject_' + item.id + '.png')"
              alt
            />
            <span class="headline_title">{{ item.name }}</span>
          </div>
          <div class="headline_two clearfix">
            <ul class="clearfix">
              <li
                :class="subject_id == sub.id ? 'active' : ''"
                v-for="(sub, index) in item.children"
                :key="index"
                @click="subClick(sub)"
              >
                <span class="headline_two_title">{{ sub.name }}</span>
              </li>
            </ul>
          </div>
        </div>
      </van-popup>
    </div>
  </div>
</template>

<script>
import { handleTree } from '@/utils'
import { getSubjectList } from '@/api/user'
export default {
  props: {},
  data() {
    return {
      subject_id: '2',
      subject_name: '临床执业',
      showKong: false,
      subjectList: []
    }
  },

  computed: {},

  mounted() {
    this.subject_id = this.$route.query.subject_id || '2'
    this.getSubject()
  },
  methods: {
    getSubject() {
      getSubjectList().then(res => {
        this.subjectList = handleTree(res.data)
        console.log(this.subjectList)
        this.subjectList.forEach(j => {
          if (j.id == this.subject_id) {
            this.subject_name = j['name']
          } else {
            if (j.children) {
              j.children.forEach(b => {
                if (b.id == this.subject_id) {
                  this.subject_name = b['name']
                }
              })
            }
          }
        })
      })
    },
    subClick(row) {
      this.$router.push({ query: { subject_id: row.id } })
      location.reload()
    }
  }
}
</script>

<style lang="scss" scoped>
.qiehuan {
  display: flex;
  justify-content: space-between;
  height: 48px;
  align-items: center;
  padding: 0 15px;
  border-bottom: 1px solid #f0f0f0;
  .name {
    font-size: 16px;
    font-weight: 400;
    color: #333333;
  }
}
.subject_popup {
  .van-popup {
    height: 385px;
    padding-bottom: 50px;
    border-radius: 10px 10px 0px 0px;
  }
  .headline {
    margin-top: 22px;
    display: flex;
    span {
      display: table-cell;
    }
    .headline_ico {
      width: 25px;
      height: 25px;
      background: #e5f4f4;
      border-radius: 50%;
    }
    .headline_title {
      padding-left: 10px;
      font-size: 15px;
      font-weight: bold;
      color: #333333;
      line-height: 30px;
    }
  }

  .headline_two {
    padding: 0 15px;
    li {
      float: left;
      width: 48%;
      height: 38px;
      margin-top: 10px;
      line-height: 38px;
      text-align: center;
      background: #f6f6f6;
      border-radius: 19px;
      .headline_two_title {
        font-size: 14px;
        font-weight: 500;
        color: #333333;
      }
    }
    li:nth-of-type(odd) {
      float: left;
    }
    li:nth-of-type(even) {
      float: right;
    }
  }
  .active {
    background: #009697 !important;
    .headline_two_title {
      color: #ffffff !important;
    }
  }
}
</style>
